package jtk.project4.fleet.task;

import java.util.List;

import jtk.project4.fleet.domain.Po;
import jtk.project4.fleet.domain.PoD;
import jtk.project4.fleet.event.AddEditPurchaseOrderEvent;
import jtk.project4.fleet.event.AddEditPurchaseOrderEvent.AddEditPurchaseOrderEventType;
import jtk.project4.fleet.service.DatabaseService;
import nl.coderight.jazz.Task;
import nl.coderight.jazz.event.MessageEvent;
import nl.coderight.jazz.event.ProgressEvent;
import nl.coderight.jazz.event.MessageEvent.MessageEventType;
import nl.coderight.jazz.event.ProgressEvent.ProgressEventType;

public class LoadPoDTask extends Task<List<PoD>>{

	@Override
	public List<PoD> process() throws Exception {
		// TODO Auto-generated method stub
		return DatabaseService.getInstance().getPoDs();
	}
	
	@Override
	protected void onStart() {
		ProgressEvent progressEvent = new ProgressEvent(ProgressEventType.START);
		progressEvent.setMessage("Loading contacts...");
		progressEvent.setIndeterminate(true);
		postEvent(progressEvent);
	}
	
	@Override
	protected void onFinish() {
		postEvent(new ProgressEvent(ProgressEventType.STOP));
		
		AddEditPurchaseOrderEvent AddEditPoEvent = new AddEditPurchaseOrderEvent(AddEditPurchaseOrderEventType.LOAD_SUCCESS);
		AddEditPoEvent.setPoDs(getResult());
		postEvent(AddEditPoEvent);
	}
	
	@Override
	protected void onError() {
		MessageEvent error = new MessageEvent(MessageEventType.ERROR);
		error.setMessage(getError().getMessage());
		postEvent(error);			
	}
	
}
